package com.camera2.http;

import android.content.Context;
import android.util.Log;
import com.camera2.Utils.StreamUtils;
import com.camera2.bean.ApplicationHelper;
import com.camera2.bean.InjuryBean;
import com.camera2.local.SharedPrefer;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import java.io.InputStream;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class SendReply {
    public static String voicePath;
    String TAG = "SendImage";
    static String message;
    static String token;

    public static String send1(Context context) throws Exception {

        token = SharedPrefer.ReadToken(context);
        InjuryBean bean = ((ApplicationHelper) context.getApplicationContext().getApplicationContext()).getInjuryBean();

        OkHttpClient client = new OkHttpClient();
        MultipartBody.Builder builder = new MultipartBody.Builder()
//				.setType(MediaType.parse("application/json"))
                .addFormDataPart("conclusion", bean.getConclusion())
                .addFormDataPart("createTime", bean.getCreateTime())
                .addFormDataPart("disease", bean.getDisease())
                .addFormDataPart("id", bean.getId() + "")
                .addFormDataPart("img", bean.getImg())
                .addFormDataPart("userId", bean.getUserId() + "")
                .addFormDataPart("voice", bean.getVoice());

        builder.setType(MediaType.parse("application/json"));
        MultipartBody requestBody = builder.build();

        Request request = new Request.Builder()
                .url(GlobalUrl.getInjuryUpdateUrl())
                .post(requestBody)
                .addHeader("token", token)
                .addHeader("Content-Type", "application/json")
                .build();
        Response response = client.newCall(request).execute();
        String result = response.body().string();
        System.out.println("图片结果" + result);
        JSONObject jsonObj = new JSONObject(result);
        message = jsonObj.getString("msg");

        return message;
    }

    public static String send(Context context)
            throws Exception {
        String token = SharedPrefer.ReadToken(context);
        InjuryBean bean = ((ApplicationHelper) context.getApplicationContext().getApplicationContext()).getInjuryBean();
        HttpClient client = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(GlobalUrl.getInjuryUpdateUrl());
        httpPost.setHeader("token", token);
        httpPost.addHeader("Content-Type", "application/json");
        JSONObject jsonParam = new JSONObject();

        jsonParam.put("id", bean.getId() + "");
        jsonParam.put("img", bean.getImg());
        jsonParam.put("userId", bean.getUserId() + "");
        jsonParam.put("voice", bean.getVoice());
        jsonParam.put("disease", bean.getDisease());
        jsonParam.put("conclusion", bean.getConclusion());
        jsonParam.put("createTime", bean.getCreateTime());


        StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");//解决中文乱码问题
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);

        HttpResponse response = client.execute(httpPost);
        int code = response.getStatusLine().getStatusCode();
        InputStream is = response.getEntity().getContent();
        String result = StreamUtils.readStream(is);
        Log.i("UpdatePassword", "UpdatePassword: " + result);
        JSONObject jsonObj = new JSONObject(result);
        String msg = jsonObj.getString("msg");

        return msg;
    }
}
